package com.color.admin.system.controller;


import com.color.admin.common.core.base.BaseController;
import com.color.admin.common.core.constant.Constant;
import com.color.admin.common.core.page.PageRequest;
import com.color.admin.common.core.result.ResultData;
import com.color.admin.common.logger.annotation.Log;
import com.color.admin.system.entity.BaseEntity;
import com.color.admin.system.entity.SysDept;
import com.color.admin.system.entity.query.DeptQueryCriteria;
import com.color.admin.system.servie.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author HaiQiu
 * @date 2021/12/23
 */
@RequestMapping("/system/dept")
@RestController
@Tag(name = "dept-controller",description = "部门管理相关接口")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    @Log("部门新增")
    @Operation(summary = "部门新增",description = "部门新增接口")
    @PostMapping
    public ResultData save(@RequestBody @Validated SysDept sysDept){
        return decide(Constant.INSERT,deptService.saveRes(sysDept));
    }

    @Log("部门修改")
    @Operation(summary = "部门修改",description = "部门修改接口")
    @PutMapping
    public ResultData update(@RequestBody @Validated(BaseEntity.update.class) SysDept sysDept){
        return decide(Constant.UPDATE,deptService.updateRes(sysDept));
    }

    @Log("部门查询")
    @Operation(summary = "部门查询",description = "部门根据ID查询数据")
    @GetMapping
    public ResultData get(@RequestParam String deptId){
        return decide(deptService.get(deptId));
    }

    @Log("部门删除")
    @Operation(summary = "部门删除",description = "部门批量删除接口")
    @DeleteMapping
    public ResultData delete(@RequestBody Set<String> deptIds){
        deptService.delete(deptIds);
        return decide(Constant.DELETE);
    }

    @Log("查询所有部门")
    @Operation(summary = "查询所有部门",description = "查询所有部门")
    @GetMapping("/all")
    public ResultData deptAll(){
        return decide(deptService.deptAll());
    }

    @Log("树形部门")
    @Operation(summary = "树形部门",description = "查询树形结构所有部门")
    @GetMapping("/tree")
    public ResultData tree(){
        return decide(deptService.tree());
    }

    @Log("部门分页模糊查询")
    @Operation(summary = "分页查询",description = "部门分页模糊查询")
    @PostMapping("/list")
    public ResultData list(@RequestBody PageRequest<DeptQueryCriteria> request){
        return decide(deptService.list(request));
    }
}
